<template>
  <div class="login">
      <input v-model="username" type="text" name="admin" placeholder="用户名">
      <input v-model="password" type="text" name="pwd" placeholder="密码">
      <input @click="login()" type="submit" value="登陆">
    <span @click="register()" class="register">注册</span>
  </div>
</template>
<script>
export default {
  data() {
    return {
      username:'',
      password:''
    }
  },
  methods: {
    login(){
      if(this.username && this.password){
        this.$axios({
          method:'post',
          url:'login',
          data:{
            username:this.username,
            password:this.password
          }
        }).then(res=>{
          if(res.data.code==4 && res.data.msg=='登录成功'){
            localStorage.setItem('token',res.data.token)
            this.$router.push({name:'home'})
          }else{
            this.$weui.topTips('登录失败: '+res.data.msg,3000)
          }
        })
      }else{
        var topTips = this.$weui.topTips('账号密码不能为空',3000)
        // topTips.hide()
      }
      
    },
    register(){
      this.$router.push({name:'register'})
    }
  },
}
</script>
<style scoped>
  .login{
    width: 80%;
    margin: 60px auto;
  }
  .login input{
    border: 1px solid gainsboro;
    width: 100%;
    height: 32px;
    margin: 20px auto;
  }
  .login input[type="submit"]{
    background-color: brown;
    color: white;
  }
  .register{
    display: block;
    text-align: center;
  }
</style>